__author__ = 'Muscle'
import time
import pyglet
import sys
import os
import mui
import mui.muiwindow
import mui.muievent
import mui.muicomponent
import mui.InternalUse.muicommon as muicommon

import mui.muianim
import mui.InternalUse.muiconfig as muiconfig
from mui import color
from mui.muianim import AnimTask
from mui.muianim import AnimLinear
from mui.muianim import AnimSpring
from mui.muianim import AnimDecay
from mui.muianim import AnimQuantum

from mui.muicomponent import MUIComponent
from mui.Widgets.muiimage import MUIImage
from mui.Widgets.muitext import MUIText
from mui.Widgets.muibutton import *

from mui.muievent import MUIEventDispatcher

import pyglet.window.mouse
import pyglet.window.key


MOUSE = pyglet.window.mouse
Key = pyglet.window.key




class myWindow(mui.muiwindow.MUIWindow):
    def __init__(self, *args, **kwargs):
        super(myWindow, self).__init__(*args, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'
        self.group = MUIGroup(self, left = 0, top = 0, width = self.width, height = self.height)
        
        self.muicomp = MUIRadioButton(self.group, left = 20, top = 20, width = 200, height = 30, name = 'btn', text = 'radio btn 1', fontColor = color.white, image = 'pyglet.png')
        self.muicomp2 = MUIRadioButton(self.group, left = 20, top = 60, width = 200, height = 30, name = 'btn', text = 'radio btn 2', fontColor = color.white, image = 'pyglet.png')
        self.muicomp3 = MUIComponent(self, left = 300, top = 300, width = 30, height = 30, bgColor = (128,128,128,255))
        self.muicomp4 = MUIImage(self, left = 400, top = 300, width = 200, height = 160, image = 'testImg.jpg', stretch = 'Uniform', bClip = False)

        self.muicomp5 = MUIText(self, left = 400, top = 100, width = 120, height = 60, text = 'MUI Text', fontColor = color.white,)

        self.autoRemove(self.muicomp.bind('Mouse Up', self.mouseDownUI1 ))
        self.autoRemove(self.muicomp3.bind('Mouse Up', self.mouseDownUI3  ))

        self.bindCmd('Key', self.testCmd, postevent = True)
        self._anim1 = None
        self._anim2 = None
        
    def mouseDownUI1(self, muicomp, x, y, buttons, modifiers):
        l = self.muicomp.left
        animTask = AnimTask(self.muicomp, 'left', AnimDecay([0, 0.5, 1.0], [l, 300, l], 5.0, bLoop=True))

        if (animTask.start()):
            self._anim1 = animTask

        animTask2 = AnimTask(self.muicomp4, 'imageOpacity',AnimDecay([0, 0.5, 1.0], [0.0, 1.0, 0.0], 2.0, bLoop=True))
        animTask2.start()

    def mouseDownUI2(self, muicomp, x, y, buttons, modifiers):
        height = self.muicomp.height
        animTask = AnimTask(self.muicomp, 'height',AnimSpring([0, 0.5, 1.0], [height, int(1.5*height), height], 1.0, bLoop=False))
        animTask.start()
        self._anim2 = animTask

    def mouseDownUI3(self, muicomp, x, y, buttons, modifiers):
        imgList = []
        baseStr = 'function\\scan\\scan_'
        for i in range(1, 13):
            _path = baseStr
            strInt = str(i)
            while len(strInt) < 2:
                strInt = '0' + strInt
            _path = baseStr + strInt + '.png'
            imgList.append(_path)
        timeslice = range(0, len(imgList))
        timeslice = [t / float(len(imgList)- 1.0) for t in timeslice ]

        animTask = AnimTask(muicomp, 'background', AnimQuantum(timeslice, imgList, 1.0, bLoop=True))
        animTask.start()

    def onKeyUI2(self, muicomp, msgType, key, symbol, modifiers):
        if (self._anim1 != None):
            if (symbol == Key.S):
                self._anim1.stop()
            elif (symbol == Key.D):
                self._anim1.pause()
            elif (symbol == Key.A):
                self._anim1.start()
            elif (symbol == Key.F):
                self._anim1.reset()
        muicomp.dispatch_cmd('Key', muicomp, msgType, key, symbol, modifiers)

    def testCmd(self, muicomp, msgType, key, symbol, modifiers):
        print 'testCmd Called!!'

    def onTextUI2(self, muicomp, text):
        print 'onTextUI2 called!!'

def main():
    mainwindow = myWindow(resizable = True, bgColor = (0,255,0,255), vsync = False)

    pyglet.app.run()

def doConfig():
    modpath = muicommon.module_path(main)
    moddir = os.path.dirname(modpath)
    muiconfig.g_ResourceDirs.append(moddir + "\\resourceData\\images")
    muiconfig.g_ResourceDirs.append(moddir + "\\resourceData\\UIImages")
    muiconfig.g_bShowFPS = True

if __name__ == '__main__':
    doConfig()
    main()






